package com.wangsd.web.service.impl;

import java.util.ArrayList;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.wangsd.core.util.DateUtils;
import com.wangsd.core.util.ExcelModelStyleUtil;
import com.wangsd.web.dao.WatermeterMapper;
import com.wangsd.web.model.Watermeter;
import com.wangsd.web.modelCustom.WatermeterCustom;
import com.wangsd.web.service.WatermeterService;

@Service
public class WatermeterServiceImpl implements WatermeterService {

    @Autowired
    WatermeterMapper watermeterMapper;

	@Override
	public boolean insertWatermeter(Watermeter watermeter) {
		int num=watermeterMapper.insertSelective(watermeter);
		if(num==1){
			return true;
		}
		return false;
	}

	@Override
	public boolean updateWatermeter(Watermeter watermeter) {
		int num=watermeterMapper.updateByPrimaryKeySelective(watermeter);
		if(num==1){
			return true;
		}
		return false;
	}

	@Override
	public Watermeter selectByPrimaryKey(Integer watermeterId) {
		Watermeter watermeter=watermeterMapper.selectByPrimaryKey(watermeterId);
		return watermeter;
	}

	@Override
	public WatermeterCustom selectRoomAndWatermeterInfo(int roomId) {
		WatermeterCustom watermeterCustom=watermeterMapper.selectRoomAndWatermeterInfo(roomId);
		return watermeterCustom;
	}

	@Override
	public Watermeter selectOldWatermeterInfo(int roomId) {
		Watermeter watermeter=watermeterMapper.selectOldWatermeterInfo(roomId);
		return watermeter;
	}

	@Override
	public List<WatermeterCustom> selectAllWatermeterInfo(WatermeterCustom record) {
		List<WatermeterCustom> list=watermeterMapper.selectAllWatermeterInfo(record);
		return list;
	}

	@Override
	public HSSFWorkbook exportWatermeterInfo(WatermeterCustom record) {
		HSSFWorkbook workbook = new HSSFWorkbook();
  		HSSFSheet sheet = workbook.createSheet();
  		ExcelModelStyleUtil.setTitleCellStyles(workbook, sheet);
  		List<WatermeterCustom> list=watermeterMapper.selectAllWatermeterInfo(record);
  		List<String[]> list2 = new ArrayList<String[]>();
  		list2.add(new String[]{"序号","小区名称","完整门牌",
  				"业主名","上期吨数","本期吨数","上期用量",
  				"本期用量","单价/吨","合计金额","抄表员","抄表时间"});
  		for (int i = 0; i < list.size(); i++) {  			
  			String housingName=list.get(i).getHousingName();
  			String building=list.get(i).getBuilding();
  		    String unit=list.get(i).getUnit();
  		    String room=list.get(i).getRoom();
  			String roominfoAddress=building+"栋"+unit+"单元"+room+"号";
  			String ownerName=list.get(i).getRoomOwner();
  			String oldWaterNum="";
  			if(list.get(i).getOldWaterNum()!=null){
  				oldWaterNum=String.valueOf(list.get(i).getOldWaterNum());
  			}
  			String waterNum="";
  			if(list.get(i).getWaterNum()!=null){
  				waterNum=String.valueOf(list.get(i).getWaterNum());
  			}
  			String oldUseNum="";
  			if(list.get(i).getOldUseNum()!=null){
  				oldUseNum=String.valueOf(list.get(i).getOldUseNum());
  			}
  			String useNum=""; 
            if(list.get(i).getUseNum()!=null){
            	useNum=String.valueOf(list.get(i).getUseNum());
  			}
  			String waterUnit="";
            if(list.get(i).getWaterUnit()!=0.0){
            	waterUnit=String.valueOf(list.get(i).getWaterUnit());
  			}
  			String amount="";
            if(list.get(i).getAmount()!=null){
            	amount=String.valueOf(list.get(i).getAmount());
  			}
  			String writeName=list.get(i).getWriteName(); 			
  			String createTime=DateUtils.formatDatetime(list.get(i).getCreateTime());  			
  			if(list.get(i).getCreateTime()!=null){
  				createTime=DateUtils.formatDatetime(list.get(i).getCreateTime());
  			}
  			list2.add(new String[]{String.valueOf(i+1),housingName,roominfoAddress,
  					ownerName,oldWaterNum,waterNum,oldUseNum,useNum,waterUnit,
  					amount,writeName,createTime});
  		}
  		for(int i=0;i<list2.size();i++){
  			HSSFRow row = sheet.createRow(i);
  			String[] cells = list2.get(i); 			
  			for(int j=0;j<cells.length;j++){
  				String cellValue = cells[j];
  				HSSFCell cell = row.createCell(j);
  				cell.setCellValue(cellValue);
  			}
  		}
		return workbook;
	}

	@Override
	public int deleteWatermeterInfo(int id) {
		int num=watermeterMapper.deleteByPrimaryKey(id);
		return num;
	}

	@Override
	public Watermeter selectWatermeterInfobyRoomId(WatermeterCustom watermeter) {
		List<Watermeter> watermeterNew=watermeterMapper.selectWatermeterInfobyRoomId(watermeter);		
		if(!watermeterNew.isEmpty()){
			return watermeterNew.get(0);
		}
		return null;
	}

}
